package com.hn.shopadmin.controller;

import com.hn.shopadmin.dto.UserLoginDTO;
import com.hn.shopadmin.dto.UserRegisterDTO;
import com.hn.shopadmin.service.UserService;
import com.hn.shopadmin.util.JwtTokenUtil;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import com.hn.kiss.modules.sys.shopdb.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@Api(tags = "UserController", description = "用户管理")
@RestController
@RequestMapping("/user")
@Validated
public class UserController extends BashController{
    @Autowired
    private UserService userService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "手机号/邮箱/身份证号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/login")
    public CommonResult<UserLoginDTO> login(@NotBlank(message = "用户名不能为空")String username,
                                            @NotBlank(message = "密码不能为空")String password){
        UserLoginDTO login = userService.login(username, password);
        return CommonResult.success(login);

    }

    @ApiOperation("用户注册")
    @PostMapping("/userRegister")
    public CommonResult userRegister(@Validated UserRegisterDTO userRegisterDTO, BindingResult bindingResult) throws ParmException {
        int count = userService.userRegister(userRegisterDTO);
        if(count > 0){
            UserLoginDTO login = userService.login(userRegisterDTO.getPhone(), userRegisterDTO.getPassword());
            return CommonResult.success(login);
        }
        return CommonResult.failed("注册失败");
    }

    @ApiOperation("店长注册")
    @PostMapping("/shopRegister")
    public CommonResult shopRegister(@Validated UserRegisterDTO userRegisterDTO, BindingResult bindingResult) throws ParmException {
        int count = userService.shopRegister(userRegisterDTO);
        if(count > 0){
            UserLoginDTO login = userService.login(userRegisterDTO.getPhone(), userRegisterDTO.getPassword());
            return CommonResult.success(login);
        }
        return CommonResult.failed("注册失败");
    }

    @ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    @GetMapping("/getById")
    public CommonResult<User> getById(@NotBlank(message = "id不能为空")String id){
        User user = userService.getById(id);
        return CommonResult.success(user);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/getAuthUser")
    public CommonResult<UserLoginDTO> getAuthUser(){
        String userName = getUserName();
        User userByUserName = userService.getUserByUserName(userName);
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        userLoginDTO.convertFor(userByUserName);
        return CommonResult.success(userLoginDTO);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    public CommonResult update(UserRegisterDTO userRegisterDTO){
        User user = userRegisterDTO.convertToUser();
        int count = userService.update(user);
        if (count > 0){
            return CommonResult.success("用户信息修改成功");
        }
        return CommonResult.failed("用户信息修改失败");
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/updatePwd")
    public CommonResult updatePwd(@NotBlank(message = "id不能为空") String id,
                                  @NotBlank(message = "password不能为空")String oldPassword,
                                  @NotBlank(message = "password不能为空")String password){
        //校验旧密码
        boolean b = userService.valedatorPassword(id, oldPassword);
        if (!b){
            return CommonResult.failed("旧密码错误");
        }


        User user = new User();
        user.setPassword(password);
        user.setId(id);
        int count = userService.update(user);
        if (count > 0){
            return CommonResult.success("密码修改成功");
        }
        return CommonResult.failed("密码修改失败");

    }


}
